const express = require("express");

const mysqla = require('mysql');
const app = express();
app.use(express.static('www'));
const mysql = mysqla.createConnection({
    
    host: 'localhost',//ip
    user: 'root',//用户名
    password: 'root',//密码
    database: 'yuedu'//要连接的数据库名称
})
mysql.connect(function (err) {
    if (err) {
        console.log("连接数据库失败");
    }
})

app.get("/listref", function (req, res) {
    console.log(req.query); //{tab:0 , limit: 20, page:1}
    var { tab, limit, page } = req.query;
    // page 1   1
    // page 2   21
    // page 3   41
    var num = (page - 1) * limit + 1;

    if (tab == 0) {
        var sql = `select * from t_articles limit ${num},${limit}`
    } else {
        var sql = `select * from t_articles where type_id=${tab} limit ${num},${limit}`
    }

    mysql.query(sql, function (err, data) {
        if (err) {
            res.json({ success: 0, data: "获取失败" })
        } else {
            res.json({ success: 1, data })
        }
    })


})
app.get('/xiang', function (req, res) {
    console.log(req.query);//{id:}
    var { id } = req.query;
    var sql = `select * from t_articles where id=${id}`
    mysql.query(sql, function (err, data) {
        if (err) {
            res.json({ success: 0, data: '获取失败' })
        } else {
            console.log(data);
            res.json({ success: 1, data })
        }

    })
})



app.listen(3000, function () {
    console.log('http://127.0.0.1:3000');
})